package org.dbstar.scheduler.beans;

import java.util.Map;

import org.dbstar.scheduler.identifier.DefaultIdentifier;
import org.dbstar.scheduler.identifier.Identifier;

import net.sf.json.JSONObject;


/**
 * 循环执行子Job的Job定义
 * 
 * @author 代波
 * @since 1.0.0
 * @version 1.0.0
 * @date 2012-5-4
 */
public class LoopJobConf extends JobConf {
	private static final long serialVersionUID = -2584017290572778446L;

	public static final String JOB_TYPE = "loop";

	/**
	 * 循环子Job定义
	 */
	private JobDefine subJob;

	private String loopVar;
	private String loopType;
	private String loopFrom;
	private String loopTo;
	private int loopStep;
	private boolean loopDependency;

	@Override
	public final String getType() {
		return JOB_TYPE;
	}

	public final JobDefine getSubJob() {
		return subJob;
	}

	public final void setSubJob(Identifier jobIdentifier, Map<String, String> parameters, String condition) {
		this.subJob = new JobDefine(jobIdentifier, parameters, null, condition, false);
	}

	public String getLoopVar() {
		return loopVar;
	}

	public void setLoopVar(String loopVar) {
		this.loopVar = loopVar;
	}

	public final String getLoopType() {
		return loopType;
	}

	public final void setLoopType(String loopType) {
		this.loopType = loopType;
	}

	public final String getLoopFrom() {
		return loopFrom;
	}

	public final void setLoopFrom(String loopFrom) {
		this.loopFrom = loopFrom;
	}

	public final String getLoopTo() {
		return loopTo;
	}

	public final void setLoopTo(String loopTo) {
		this.loopTo = loopTo;
	}

	public final int getLoopStep() {
		return loopStep;
	}

	public final void setLoopStep(int loopStep) {
		this.loopStep = loopStep;
	}

	public final boolean isLoopDependency() {
		return loopDependency;
	}

	public final void setLoopDependency(boolean loopDependency) {
		this.loopDependency = loopDependency;
	}

	@Override
	public final String getContent() {
		JSONObject object = new JSONObject();
		object.element("subJob", buildJsonJob(subJob));
		object.element("loopVar", loopVar);
		object.element("loopType", loopType);
		object.element("loopFrom", loopFrom);
		object.element("loopTo", loopTo);
		object.element("loopStep", loopStep);
		object.element("loopDependency", loopDependency);
		return object.toString();
	}

	private static JSONObject buildJsonJob(JobDefine define) {
		JSONObject obj = new JSONObject();
		obj.element("identifier", define.getIdentifier().getStringIdentifier());
		Map<String, String> parameters = define.getParameters();
		if (parameters != null && !parameters.isEmpty()) {
			obj.element("parameters", parameters);
		}
		if (define.getCondition() != null) {
			obj.element("condition", define.getCondition());
		}
		return obj;
	}

	@SuppressWarnings("unchecked")
	@Override
	public final void setContent(String content) {
		JSONObject obj = JSONObject.fromObject(content);
		JSONObject sub = obj.getJSONObject("subJob");
		Identifier identifier = DefaultIdentifier.valueOf(sub.getString("identifier"));
		Map<String, String> parameters = (Map<String, String>) sub.opt("parameters");
		String condition = sub.optString("condition", null);
		setSubJob(identifier, parameters, condition);
		this.loopVar = obj.getString("loopVar");
		this.loopType = obj.getString("loopType");
		this.loopFrom = obj.getString("loopFrom");
		this.loopTo = obj.getString("loopTo");
		this.loopStep = obj.getInt("loopStep");
		this.loopDependency = obj.getBoolean("loopDependency");
	}

	@Override
	public JobConf clone() throws CloneNotSupportedException {
		LoopJobConf conf = (LoopJobConf) super.clone();
		conf.subJob = subJob.clone();
		return conf;
	}
}
